﻿using AlibabaCloud.CallCenter;
using AlibabaCloud.CallCenter.Models;
using AlibabaCloud.Framwork;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Text;
using System.Web.Http;
using System.Web.ModelBinding;
using Tea;
using Tea.Utils;

namespace WebApplication2.Controllers
{
    [RoutePrefix("Ccc")]
    public class CccController : ApiController
    {
        [Route("sseApi")]
        [HttpPost]
        public HttpResponseMessage sseApi(string action, string product, string version, string region, JObject jsonObj)
        {
            //此处接收 request 参数
            var jsonStr = JsonConvert.SerializeObject(jsonObj); //序列化为json字符串
            dynamic jsonParams = JsonConvert.DeserializeObject<dynamic>(jsonStr); //反序列化为动态json object
            string requestStr = jsonParams.request;
            Dictionary<string, object> formData = new Dictionary<string, object>();

            //因为接收到的  request 参数 重的参数 第一个字符是小写  而后台的参数要把第一个字符转成大写
            Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(requestStr);
            foreach (var dic in dict)
            { 
                string newKey = dic.Key.Substring(0, 1).ToUpper() + dic.Key.Substring(1, dic.Key.Length - 1);
                formData.Add(newKey, dic.Value);
            }

            //string accessKeyId = "";
            //string accessKeySecret = "";
            //HttpHelper httpHelper = new HttpHelper(accessKeyId, accessKeySecret);
            //var result1 = httpHelper.CallApi(action, product, null, formData, null, "POST", version, region);

            string json = "";
            var response = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Common.GetActionResult(action, version, formData)));

            //这里只返回 接口数据中的 body
            //json = JsonConvert.SerializeObject(response["body"]);

            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };
            return result;
        }
    }
}
